package com.toommi.refuse.controller;

import com.toommi.refuse.annotation.PermissionController;
import com.toommi.refuse.annotation.PermissionReport;
import com.toommi.refuse.common.response.CommonResponse;
import com.toommi.refuse.entity.LargeRecyclePlace;
import com.toommi.refuse.service.LargeRecyclePlaceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

/**
 * @author yangg
 * @since 2019-07-31 15:32
 */
@RestController
@RequestMapping(value = "")
@Slf4j
@AllArgsConstructor
@PermissionController(groupCode = "admin_large_recycle_place", groupName = "大件垃圾回收点管理")
@Api(tags = "大件垃圾回收点管理")
public class LargeRecyclePlaceController {


    private LargeRecyclePlaceService largeRecyclePlaceService;

    /**
     * 新增大件垃圾回收点
     *
     * @param place 回收点信息
     * @return 操作结果
     */
    @ApiOperation(value = "新增大件垃圾回收点", notes = "新增大件回收点")
    @ApiImplicitParam(name = "place", value = "大件回收点实体", required = true, dataType = "LargeRecyclePlace")
    @RequiresPermissions("admin:largerecycle:place:add")
    @PermissionReport(value = "admin:largerecycle:place:add", name = "新增回收点", group = "admin_large_recycle_place")
    @PostMapping(value = "/admin/large/recycle/place", produces = "application/json;charset=UTF-8")
    public CommonResponse addPlace(@RequestBody LargeRecyclePlace place) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            largeRecyclePlaceService.addLargeRecyclePlace(place);
        } catch (Exception e) {
            log.error("add large recycle place error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 删除回收点
     *
     * @param id 回收点id
     * @return 删除结果
     */
    @ApiOperation(value = "删除大件垃圾回收点", notes = "删除大件回收点")
    @ApiImplicitParam(name = "id", value = "大件回收点id", required = true, dataType = "long")
    @RequiresPermissions("admin:largerecycle:place:delete")
    @PermissionReport(value = "admin:largerecycle:place:delete", name = "删除回收点", group = "admin_large_recycle_place")
    @DeleteMapping(value = "/admin/large/recycle/place/{id}", produces = "application/json;charset=UTF-8")
    public CommonResponse deletePlace(@PathVariable(name = "id") long id) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            largeRecyclePlaceService.deleteLargeRecyclePlace(id);
        } catch (Exception e) {
            log.error("delete large recycle place error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

    /**
     * 分页获取回收点信息
     *
     * @param pageNum  页码
     * @param pageSize 每页数据量
     * @param keyWord  查询关键字（回收点名称）
     * @return 回收点信息
     */
    @ApiOperation(value = "查询大件垃圾回收点", notes = "查询大件回收点")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页数据量", required = true, dataType = "int"),
            @ApiImplicitParam(name = "keyWord", value = "查询关键字", required = true, dataType = "String")
    })

    @GetMapping(value = "/api/large/recycle/places", produces = "application/json;charset=UTF-8")
    public CommonResponse findPlaces(int pageNum, int pageSize, String keyWord) {

        CommonResponse commonResponse = CommonResponse.success();

        try {
            commonResponse.setData(largeRecyclePlaceService.findLargeRecyclePlace(pageNum, pageSize, keyWord).getList());
        } catch (Exception e) {
            log.error("get large recycle place error:{}", e);
            commonResponse = CommonResponse.serverError();
        }
        return commonResponse;
    }

}
